﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DomainModel;

namespace BL
{
    public static class BLCustomers
    {

        /*
         InsertCustomer: checking input before calling actual method
         * Parm1: customer
         * Parm2: erros
         */
        public static void InsertCustomer(BaseCustomer customer , ref List<string> errors)
        {
            int id = 0;
            bool duplicate = false;
            if (!BLTool.isNull(customer))
            {
                String email = GetEmail(id, ref errors);
                //logic to make sure no 2 emails are the same 
                while (email != null)
                {
                    if (customer.email == email)
                    {
                        duplicate = true;
                    }
                    id = id + 1;
                    email = GetEmail(id, ref errors);
                }
            }
            if (BLTool.isNull(customer)) 
            {
                BLMsgPool.ErrorCapsule(1,  1 , ref errors);
            }
            else if (duplicate)
            {
                BLMsgPool.ErrorCapsule(3, 6, ref errors);
            }
            else if (BLTool.isRightEmail(customer.email))
            {
                BLMsgPool.ErrorCapsule(1, 12, ref errors);
            }
            else if (string.IsNullOrEmpty(customer.first_name))
            {
                BLMsgPool.ErrorCapsule(1, 2, ref errors);
            }
            else if (BLTool.isEmptyString(customer.last_name))
            {
                BLMsgPool.ErrorCapsule(1, 3, ref errors);
            }
            else if (BLTool.isEmptyString(customer.address))
            {
                BLMsgPool.ErrorCapsule(1, 4, ref errors);
            }
            else if (BLTool.isEmptyString(customer.state))
            {
                BLMsgPool.ErrorCapsule(1, 5, ref errors);
            }
            else if (BLTool.isNotZipCode(customer.zip))
            {
                BLMsgPool.ErrorCapsule(1, 6, ref errors);

            }
       /*     else if (!(BLTool.isBinary(customer.webmaster)))
            {
                BLMsgPool.ErrorCapsule(1, 7, ref errors);
            } */
            else if (!(BLTool.isBinary(customer.blockuser)))
            {
                BLMsgPool.ErrorCapsule(1, 8, ref errors);
            }
            else
            {
                DalCustomers.InsertCustomer(customer, ref errors);
            }
            return;
        }




        public static void BlockUser(int userID, ref List<string> errors)
        {
            if (BLTool.isNagetive(userID))
            {
                BLMsgPool.ErrorCapsule(1, 10, ref errors);
            }

            else 
            {
                DalCustomers.BlockCustomer(userID , ref  errors);
            }
            return;
        }

        public static void ChangePermission(BaseCustomer customer ,  ref List<string> errors)
        {

            if(BLTool.isNull(customer))
            {
                BLMsgPool.ErrorCapsule(1, 1, ref errors);
            }
            else if(!BLTool.isBinary(customer.webmaster))
            {
                BLMsgPool.ErrorCapsule(1, 9, ref errors);
            }
            else if (BLTool.isNagetive(customer.customer_id))
            {
                BLMsgPool.ErrorCapsule(1, 10, ref errors);
            }
            else 
            {
                DalCustomers.ChangePermissions(customer.customer_id, customer.webmaster , ref errors);
            }
            return;
        }

  
        public static void UpdateUserReden(int id, BaseCustomer UpdateList, ref List<string> errors)
        {
            if (BLTool.isNotValidID(id))
                BLMsgPool.ErrorCapsule(2, 1, ref errors);
            else if (BLTool.isNull(UpdateList))
                BLMsgPool.ErrorCapsule(2, 6, ref errors);
            else if (BLTool.isEmptyString(UpdateList.address))
                BLMsgPool.ErrorCapsule(2, 2, ref errors);
            else if (BLTool.isEmptyString(UpdateList.state))
                BLMsgPool.ErrorCapsule(2, 3, ref errors);
            else if (BLTool.isNotZipCode(UpdateList.zip))
                BLMsgPool.ErrorCapsule(2, 4, ref errors);
            else if (BLTool.isEmptyString(UpdateList.city))
                BLMsgPool.ErrorCapsule(2, 5, ref errors);
            else
                DalCustomers.UpdateUserReden(id, UpdateList, ref errors);
        }

        public static void UnBlockCustomer(int id, ref List<string> errors)
        {
            if (BLTool.isNotValidID(id))
                BLMsgPool.ErrorCapsule(2, 1, ref errors);
            else
                DalCustomers.UnBlockCustomer(id, ref errors);
        }

        public static List<BaseCustomer> GetName(ref List<string> errors)
        {
            return DalCustomers.GetName(ref errors);
        }

        public static BaseCustomer getId(BaseCustomer customer, ref List<string> errors)
        {
            if (BLTool.isNull(customer))
            {
                BLMsgPool.ErrorCapsule(2, 6, ref errors);
                return null;
            }
            else if (BLTool.isEmptyString(customer.first_name))
            {
                BLMsgPool.ErrorCapsule(2, 7, ref errors);
                return null;
            }
            else if (BLTool.isEmptyString(customer.last_name))
            {
                BLMsgPool.ErrorCapsule(2, 8, ref errors);
                return null;
            }
            
               return DalCustomers.getId(customer, ref errors);
        }

        //get id from email
        public static int GetIdFromEmail(string email, ref List<string> errors)
        {

            return DalCustomers.GetIdFromEmail(email, ref errors);
        }

        public static String GetEmail(int id, ref List<string> errors)
        {
            if (BLTool.isNotValidID(id))
                BLMsgPool.ErrorCapsule(2, 1, ref errors);
            
               return DalCustomers.GetEmail(id, ref errors);
        }

        public static BaseCustomer GetCustomerDetail(int id, ref List<string> errors)
        {
            if (BLTool.isNotValidID(id))
            {
                BLMsgPool.ErrorCapsule(2, 1, ref errors);
                return null;
            }
            else
                return DalCustomers.GetCustomerDetail(id, ref errors);
        }

        public static void DeleteCustomer(int id, ref List<string> errors)
        {
            if (BLTool.isNotValidID(id))
                BLMsgPool.ErrorCapsule(2, 1, ref errors);
            else
                DalCustomers.DeleteCustomer(id, ref errors);
        }
        /*
            Checking SQL TABLE  for user or webmaster login 
         */
        public static BaseCustomer LoginInCheck(String email, String firstname, ref List<string> errors)
        {
            if(String.IsNullOrEmpty(email))
            {
                BLMsgPool.ErrorCapsule(14 , 1, ref errors);
                return null;
               
            }
            if(BLTool.isRightEmail(email))
            {
                BLMsgPool.ErrorCapsule( 12 , 1, ref errors);
                return null;
            }
            if(String.IsNullOrEmpty(firstname))
            {
                BLMsgPool.ErrorCapsule(15 , 1, ref errors);
                return null;
            }
            else
                return DalCustomers.LoginInCheck(email , firstname , ref errors);

        }



        /*
            Displaying part of wbemaster: 
         */
        public static int GetTotalUser(ref List<string> errors)
        {
            return DalCustomers.GetTotalUser(ref errors);
        }

        public static int GetTotalWebMaster(ref List<string> errors) 
        {
            return DalCustomers.GetTotalWebMaster(ref errors);
        }

        public static int GetTotalBlock(ref List<string> errors)
        {
            return DalCustomers.GetTotalBlock(ref errors);
        }

        public static List<BaseCustomer> GetBlockUserInfor(ref List<string> errors) 
        {

            return DalCustomers.GetBlockUserInfor(ref errors);
        }

        public static List<BaseCustomer> GetWebMasterInfor(ref List<string> errors) 
        {
            return DalCustomers.GetWebMasterInfor(ref errors);
        }
        /*
            End of Webmamster page dispay information
         */


    }
}
